package com.es.disped.web.controller.oj.conteststudent;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.es.disped.api.oj.contest.HustContestService;
import com.es.disped.api.oj.conteststudent.HustContestStudentService;
import com.es.disped.common.annotation.DynamicDataSource;
import com.es.disped.common.constant.DataSourceName;
import com.es.disped.common.model.MsgModel;
import com.es.disped.common.spring.DataSourceContextHolder;
import com.es.disped.core.controller.BaseController;
import com.es.disped.dao.model.ContestStudentRel;
import com.es.disped.dao.model.ContestStudentRelExample;
import com.es.disped.dao.oj.model.Contest;
import com.es.disped.dao.oj.model.ContestExample;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author Anson<br>
 *	Copyright by EasyShare 2019<br>
 *  
 *  All right reserved<br>
 *
 *  Created on 2019年7月18日 下午12:07:58<br>
 *  名称：ContestStudentController.java<br>
 *  描述：竞赛及作业学生关系管理<br>
 */
@Api("竞赛及作业学生关系管理")
@Controller
@RequestMapping("/manage/oj/contest/student")
public class ContestStudentController extends BaseController{

	@Autowired
	HustContestStudentService hustContestStudentService;
	@Autowired
	HustContestService hustContestService;
	
	@ApiOperation(value="为学生分配给竞赛及作业对象",response=MsgModel.class,httpMethod="POST",notes="为竞赛及作业添加学生")
	@RequiresPermissions(value={"teacher:save","admin:save"},logical=Logical.OR)
	@RequestMapping(value="/save",method=RequestMethod.POST)
	@Transactional
	@DynamicDataSource
	public @ResponseBody MsgModel save(@RequestBody @ApiParam(name="contestStudentRels",value="竞赛及作业学生对象",required=true)
										List<ContestStudentRel> contestStudentRels)
	{
		int count=0;
		for(ContestStudentRel record:contestStudentRels)
		{
			if(Optional.of(record)
					.filter(cId->record.getContestId()!=null)
					.filter(sId->record.getStudentId()!=null)//ESH平台对象的用户ID
					.filter(spe->record.getStudentPhone()!=null)//OJ系统对应的用户ID
					.isPresent())
			{
				record.setRelId(getUUID());
				count+=hustContestStudentService.insertSelective(record);
			}
		}
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"竞赛作业学生添加成功":"竞赛作业学生添加失败",count>=1?contestStudentRels:"");
	}
	
	
	@ApiOperation(value="删除竞赛及作业对象关联的学生",response=MsgModel.class,httpMethod="POST",notes="删除为竞赛及作业添加的学生")
	@RequiresPermissions(value={"teacher:delete","admin:delete"},logical=Logical.OR)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public @ResponseBody MsgModel delete(@RequestBody @ApiParam(name="contestStudentRels",value="竞赛及作业学生对象",required=true)
										List<ContestStudentRel> contestStudentRels)
	{
		int count=0;
		List<String> sIds=new ArrayList<String>();
		for(ContestStudentRel record:contestStudentRels)
		{
			if(!this.isNull(record.getStudentId()))
			{
				sIds.add(record.getStudentId());
			}
		}
		ContestStudentRelExample deleteExample=new ContestStudentRelExample();
		ContestStudentRelExample.Criteria criteria=deleteExample.createCriteria();
		criteria.andStudentIdIn(sIds);
		count=hustContestStudentService.deleteByExample(deleteExample);
		return this.resultMsg(count>=1?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				count>=1?"竞赛作业学生删除成功":"竞赛作业学生删除失败",count>=1?contestStudentRels:"");
	}
	
	
	@ApiOperation(value="根据学生ID查询学生竞赛及作业列表",response=MsgModel.class,httpMethod="POST",notes="加载学生个人相关的竞赛及作业")
	@RequestMapping(value="/select",method=RequestMethod.POST)
	public @ResponseBody MsgModel getStudentContest(@RequestBody @ApiParam(name="contestStudentRel",value="竞赛及作业学生对象",required=true)
										ContestStudentRel contestStudentRel)
	{
		List<ContestStudentRel> contestStudentRels=null;
		if(!this.isNull(contestStudentRel.getStudentId()))
		{
			ContestStudentRelExample selectExample=new ContestStudentRelExample();
			ContestStudentRelExample.Criteria criteria=selectExample.createCriteria();
			criteria.andStudentIdEqualTo(contestStudentRel.getStudentId());
			contestStudentRels=hustContestStudentService.selectByExample(selectExample);
		}
		ContestExample contestExample=new ContestExample();
		ContestExample.Criteria criteria=contestExample.createCriteria();
		List<Integer> cIds=new ArrayList<Integer>();
		if(!this.isNull(contestStudentRels))
		{
			for(ContestStudentRel record:contestStudentRels)
			{
				if(!this.isNull(record.getContestId()))
				{
					cIds.add(record.getContestId());
				}
			}
		}
		criteria.andContestIdIn(cIds).andDefunctEqualTo("N");
		DataSourceContextHolder.setDataSource(DataSourceName.EXTEND.getName());
		List<Contest> contests=hustContestService.selectByExample(contestExample);
		DataSourceContextHolder.clearDataSource();
		return this.resultMsg(!this.isNull(contests)?HttpStatus.OK.value():HttpStatus.EXPECTATION_FAILED.value(),
				!this.isNull(contests)?"学生作业加载成功":"学生作业加载失败",!this.isNull(contests)?contests:"");
	}
}
